using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Marlimer.Logical.Comercial;
using Marlimer.Logical.Universal;
using Marlimer.Logical.Sistema;
using Marlimer.Common;
using Marlimer.Logistico.Reportes.Crystals; 

namespace Marlimer.Logistico.Reportes.Formularios
{
    public partial class frm_DevolucionXTransportista : Form
    {

        lcom_Transportista lcom_transportista = new lcom_Transportista();
        List<InfoParamRpt> paramsrpt = new List<InfoParamRpt>();
        rptDevolucionXTransportista RptDevolucionXTransportista = new rptDevolucionXTransportista();
        rptDevolucionXTransportistaResumen RptDevolucionXTransportistaResumen = new rptDevolucionXTransportistaResumen();

        public frm_DevolucionXTransportista()
        {
            InitializeComponent();
        }

        private void frm_DevolucionXTransportista_Load(object sender, EventArgs e)
        {
            CargarCombos();

            dtpFecEnt.Value = DateTime.Today;

            this.Location = new Point(0, 0);
        }

        private void CargarCombos()
        {
            Util.LlenarDataCombo(lcom_transportista.getDTTransportista(), cboTransportista, true);
        }

        private void cboGeneral_KeyPress(object sender, KeyPressEventArgs e)
        {
            Util.validaEnter(e.KeyChar);
        }

        private void txtGeneral_Leave(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            String value = "";
            int valuenum = -1;
            String nametxt = txt.Name;
            object newval = null;

            if (txt.Text.Trim() != "")
            {
                value = txt.Text;
                valuenum = Convert.ToInt32(Util.ToValor(txt.Text));
            }

            switch (nametxt)
            {
                case "txtCodTransportista":
                    {
                        cboTransportista.SelectedValue = valuenum;
                        newval = cboTransportista.SelectedValue; 
                        break;
                    }
            }
            txt.Text = Convert.ToString(newval);
        }

        private void cboGeneralDT_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cbo = (ComboBox)sender;
            String valor = "";
            String namecbo = cbo.Name;
            //validamos que el indice seleccionado sea mayor a -1
            if (cbo.SelectedIndex > -1 && cbo.SelectedValue.ToString() != "System.Data.DataRowView")
            {
                valor = Convert.ToString(cbo.SelectedValue);
            }

            switch (namecbo)
            {
                case "cboTransportista":
                    {
                        txtCodTransportista.Text = valor;
                        break;
                    }
            }

        }

        private void txtGeneralSelect_Enter(object sender, EventArgs e)
        {
            Util.selectAllText(((TextBox)sender));
        }


        private void txtGeneralEntero_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = Util.validaTecla(e.KeyChar, "Entero", ((TextBox)sender).Text, true);
        }

        private void tstComandos_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int indice = tstComandos.Items.IndexOf(e.ClickedItem);
            switch (indice)
            {
                case 0:     //nuevo
                    {
                        nuevaBusqueda();
                        break;
                    }
                case 1:     //imprimir o mostrar
                    {
                        CargarReporte();
                        break;
                    }
                case 2:
                    {
                        Util.exportReporte(RptDevolucionXTransportista); 
                        break;
                    }
            }
        }

        private void nuevaBusqueda()
        {
            CargarCombos();
            txtCodTransportista.Focus();  
        }

        private void CargarReporte()
        {
            //segun la opcion marcada
            if (rbtPorTransportista.Checked)
            {
                //verificamos que se ingresen todos los datos.

                if (cboTransportista.SelectedIndex > -1)
                {
                    paramsrpt.Clear();
                    paramsrpt.Add(new InfoParamRpt("@dfec_ent", dtpFecEnt.Value));
                    paramsrpt.Add(new InfoParamRpt("@ccod_trans", Convert.ToInt32(cboTransportista.SelectedValue)));
                    try
                    {
                        crpReporte.ReportSource = Util.setRptParams(RptDevolucionXTransportista, paramsrpt);
                        tlbExportar.Enabled = true;
                    }
                    catch (Exception ex)
                    {
                        tlbExportar.Enabled = false;
                    }
                }
                else
                {
                    MessageBox.Show("Debe seleccionar el transportista.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else if (rbtResumenPorDia.Checked)
            {
                paramsrpt.Clear();
                paramsrpt.Add(new InfoParamRpt("@dfec_ent", dtpFecEnt.Value));
                paramsrpt.Add(new InfoParamRpt("@ccod_trans", 0));
                try
                {
                    crpReporte.ReportSource = Util.setRptParams(RptDevolucionXTransportistaResumen, paramsrpt);
                    tlbExportar.Enabled = true;
                }
                catch (Exception ex)
                {
                    tlbExportar.Enabled = false;
                }
            }
        }

        private void rbtPorTransportista_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtPorTransportista.Checked)
            {
                cboTransportista.Enabled = true;
                txtCodTransportista.Enabled = true; 
            }
            else if (rbtResumenPorDia.Checked)
            {
                cboTransportista.Enabled = false;
                txtCodTransportista.Enabled = false; 
                cboTransportista.SelectedValue = -1; 
            }

        }

    }
}